package com.enzoism.springboot.dive.bis.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThreadLocalTest {

    // ThreadLocal 当前线程局部变量
    private static ThreadLocal<String> threadLocal = new ThreadLocal<String>();


    public static void main(String[] args) {
        int taskNum = 10;
        ExecutorService executorService = Executors.newFixedThreadPool(taskNum);
        for (int i = 0; i < taskNum; i++) {
            executorService.submit(new MyRunnableTask());
        }

        executorService.shutdown();
    }


    static class MyRunnableTask implements Runnable {
        @Override
        public void run() {
            // threadLocal内存储当前线程名称
            threadLocal.set(Thread.currentThread().getName());

            // threadLocal内取出之前存入的变量
            System.out.println("------>" + Thread.currentThread().getName() + ":" + threadLocal.get());
        }
    }
}
